#include "companymanagement.h"
#include "ui_companymanagement.h"
#include "global.h"

CompanyManagement::CompanyManagement(QSqlDatabase &database, QWidget *parent, Qt::WindowFlags f) :
    QWidget(parent, f),
    ui(new Ui::CompanyManagement),
    query(database)
{
    this->ui->setupUi(this);
    this->setupComboBoxes();
}

CompanyManagement::~CompanyManagement()
{
    delete ui;
}

void CompanyManagement::setupComboBoxes()
{
    this->setupComboBox(this->ui->companyNameComboBox, "投递意向", "公司名称");
    this->setupComboBox(this->ui->platformNameComboBox, "投递平台", "平台名称");
    this->setupComboBox(this->ui->loginNameComboBox, "登录方式", "登录方式名称");
    this->setupComboBox(this->ui->passwordComboBox, "密码", "密码形式");
}

void CompanyManagement::setupComboBox(QComboBox *comboBox, const QString &tableName, const QString &fieldName)
{
    query.exec(QString("select ID, %1 from %2 order by ID asc").arg(fieldName).arg(tableName));
    while (query.next())
    {
        int id = query.value("ID").toInt();
        QString text = query.value(fieldName).toString();
        comboBox->addItem(text, QVariant(id));
    }
}

void CompanyManagement::onCompanyChanged(QString text)
{
    if (this->isHidden())
        return;
    qDebug() << "onCompanyChanged";
    const QString sql = QString(
                "select 投递意向.*, "
                "投递平台.平台名称 "
                "from 投递意向 "
                "left join 投递平台 on 投递意向.投递平台ID = 投递平台.ID "
                "where 投递意向.公司名称 = '%1' "
                "order by 投递意向.ID asc ")
            .arg(text);
    QSqlQuery query(this->query);

    while (true)
    {
        query.exec(sql);
        if (query.next())
            break;
        //如果没有此元素
        qDebug()<<"NewCompany";
        query.exec(QString(
                       "insert into 投递意向(公司名称, 投递平台ID) "
                       "select '%1', 投递平台.ID "
                       "from 投递平台 "
                       "where 投递平台.平台名称 = '%2' ")
                   .arg(text)
                   .arg(this->ui->platformNameComboBox->currentText())
                   );
        this->onUpdateCompanyInfo();
    }

    this->ui->isCvSubmitedCheckBox->setChecked(query.value("是否已投").toBool());
    this->ui->cvUrlLineEdit->setText(query.value("投递网址").toString());
    this->ui->platformNameComboBox->setCurrentText(query.value("平台名称").toString());

    this->onPlatformChanged(query.value("平台名称").toString());
}

void CompanyManagement::onPlatformChanged(QString text)
{
    if (this->isHidden())
        return;
    qDebug() << "onPlatformChanged";
    const QString sql = QString(
                "select 投递平台.*, "
                "登录方式.登录方式名称 "
                "from 投递平台 "
                "left join 登录方式 on 投递平台.登录方式ID = 登录方式.ID "
                "where 投递平台.平台名称 = '%1' "
                "order by 投递平台.ID asc ")
            .arg(text);

    QSqlQuery query(this->query);

    while (true)
    {
        query.exec(sql);
        if (query.next())
            break;
        //如果没有此元素
        qDebug()<<"NewPlatform";
        query.exec(QString(
                       "insert into 投递平台(平台名称, 登录方式ID) "
                       "select '%1', 登录方式.ID "
                       "from 登录方式 "
                       "where 登录方式.登录方式名称 = '%2' ")
                   .arg(text)
                   .arg(this->ui->loginNameComboBox->currentText())
                   );
        this->onUpdatePlatformInfo();
    }

    this->ui->isPlatformCheckBox->setChecked(query.value("是否为投递平台").toBool());
    this->ui->platformUrlLineEdit->setText(query.value("平台网址").toString());
    this->ui->loginNameComboBox->setCurrentText(query.value("登录方式名称").toString());

    this->onLoginChanged(query.value("登录方式名称").toString());

    //修改上一级
    int id = query.value("ID").toInt();
    QString companyName = this->ui->companyNameComboBox->currentText();
    query.exec(QString(
                   "update 投递意向 "
                   "set 投递平台ID = %1 "
                   "where 公司名称 = '%2' ")
               .arg(id)
               .arg(companyName)
               );
}

void CompanyManagement::onLoginChanged(QString text)
{
    if (this->isHidden())
        return;
    qDebug() << "onLoginChanged";
    const QString sql = QString(
                "select 登录方式.*, "
                "密码.密码形式 "
                "from 登录方式 "
                "left join 密码 on 登录方式.密码ID = 密码.ID "
                "where 登录方式.登录方式名称 = '%1' "
                "order by 登录方式.ID asc ")
            .arg(text);

    QSqlQuery query(this->query);

    while (true)
    {
        query.exec(sql);
        if (query.next())
            break;
        //如果没有此元素
        qDebug()<<"NewLogin";
        query.exec(QString(
                       "insert into 登录方式(登录方式名称, 密码ID) "
                       "select '%1', 密码.ID "
                       "from 密码 "
                       "where 密码.密码形式 = '%2'")
                   .arg(text)
                   .arg(this->ui->passwordComboBox->currentText())
                   );
        this->onUpdateLoginInfo();
    }

    this->ui->loginUsernameLineEdit->setText(query.value("用户名").toString());
    this->ui->passwordComboBox->setCurrentText(query.value("密码形式").toString());

    this->onPasswordChanged(query.value("密码形式").toString());

    //修改上一级
    int id = query.value("ID").toInt();
    QString platformName = this->ui->platformNameComboBox->currentText();
    query.exec(QString(
                   "update 投递平台 "
                   "set 登录方式ID = %1 "
                   "where 平台名称 = '%2' ")
               .arg(id)
               .arg(platformName)
               );
}
void CompanyManagement::onPasswordChanged(QString text)
{
    if (this->isHidden())
        return;
    qDebug() << "onPasswordChanged";
    QString sql = QString(
                "select * "
                "from 密码 "
                "where 密码形式 = '%1' ")
            .arg(text);
    QSqlQuery query(this->query);

    while (true)
    {
        query.exec(sql);
        if (query.next())
            break;
        qDebug()<<"NewPassword";
        query.exec(QString(
                       "insert into 密码(ID, 密码形式) "
                       "values (NULL, '%1') ")
                   .arg(text));
        this->onUpdatePasswordInfo();
    }
    //修改上一级
    int id = query.value("ID").toInt();
    QString loginName = this->ui->loginNameComboBox->currentText();
    query.exec(QString(
                   "update 登录方式 "
                   "set 密码ID = %1 "
                   "where 登录方式名称 = '%2' ")
               .arg(id)
               .arg(loginName)
               );
}

void CompanyManagement::onUpdateCompanyInfo()
{
    if (this->isHidden())
        return;
    qDebug() << "onUpdateCompanyInfo";
    QSqlQuery query(this->query);
    query.exec(QString(
                   "update 投递意向 "
                   "set 投递网址 = '%1', "
                   "是否已投 = %2 "
                   "where 公司名称 = '%3' ")
               .arg(this->ui->cvUrlLineEdit->text())
               .arg(this->ui->isCvSubmitedCheckBox->checkState() == Qt::Checked ? 1 : 0)
               .arg(this->ui->companyNameComboBox->currentText())
               );
}
void CompanyManagement::onUpdatePlatformInfo()
{
    if (this->isHidden())
        return;
    qDebug() << "onUpdatePlatformInfo";
    QSqlQuery query(this->query);
    query.exec(QString(
                   "update 投递平台 "
                   "set 平台网址 = '%1', "
                   "是否为投递平台 = %2 "
                   "where 平台名称 = '%3' ")
               .arg(this->ui->platformUrlLineEdit->text())
               .arg(this->ui->isPlatformCheckBox->checkState() == Qt::Checked ? 1 : 0)
               .arg(this->ui->platformNameComboBox->currentText())
               );
}
void CompanyManagement::onUpdateLoginInfo()
{
    if (this->isHidden())
        return;
    qDebug() << "onUpdateLoginInfo";
    QSqlQuery query(this->query);
    query.exec(QString(
                   "update 登录方式 "
                   "set 用户名 = '%1' "
                   "where 登录方式名称 = '%2' ")
               .arg(this->ui->loginUsernameLineEdit->text())
               .arg(this->ui->loginNameComboBox->currentText())
               );
}
void CompanyManagement::onUpdatePasswordInfo()
{
    if (this->isHidden())
        return;
    qDebug() << "onUpdatePasswordInfo";
    QSqlQuery query(this->query);
}

void CompanyManagement::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    qDebug() << this->isHidden();
    this->onCompanyChanged(this->ui->companyNameComboBox->currentText());
}
